package com.gcosc.hly.codejava.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcosc.hly.codejava.VO.PageResult;
import com.gcosc.hly.codejava.VO.TopicVO;
import com.gcosc.hly.codejava.entity.Topic;
import com.gcosc.hly.codejava.entity.TopicResult;
import com.gcosc.hly.codejava.mapper.TopicMapper;
import com.gcosc.hly.codejava.params.QueryParams;
import com.gcosc.hly.codejava.service.TopicResultService;
import com.gcosc.hly.codejava.service.TopicService;
import lombok.RequiredArgsConstructor;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    private final TopicResultService resultService;

    @Override
    public List<TopicVO> getTopicVOList(QueryParams params) {
        List<TopicVO> list = listByIds(Arrays.asList(params.getIds().split(",")))
                .stream().map(n -> getTopicVO(n)).collect(Collectors.toList());
        return list;
    }

    @Override
    public TopicVO getTopicVO(Topic topic) {
        TopicVO build = TopicVO.builder()
                .topic(topic)
                .result(resultService.lambdaQuery()
                        .eq(TopicResult::getTopicId, topic.getId())
                        .list())
                .build();
        return build;
    }

    @Override
    public TopicVO detail(int id) {
        return getTopicVO(getById(id));
    }

    @Override
    public Page<Topic> queryParams(QueryParams params) {
        Page<Topic> page = lambdaQuery()
                .eq(StringUtils.isNotBlank(params.getIds()), Topic::getId, params.getIds())
                .like(StringUtils.isNotBlank(params.getTitle()), Topic::getTitle, params.getTitle())
                .eq(params.getStatus() != 0,Topic::getStatus, params.getStatus())
                .eq(params.getLevel() != 0, Topic::getLevel, params.getLevel())
                //.select(Topic::getId,
                //        Topic::getCreateTime,
                //        Topic::getLevel,
                //        Topic::getTitle
                //)
                .page(new Page<Topic>(params.getPage(), params.getLimit()));
        return page;
    }

    @Override
    public int edit(Topic topic) {
        if (topic.getId() == 0){
            save(topic);
        }else {
            updateById(topic);
        }
        return topic.getId();
    }

    @Override
    public void dels(String ids) {
        List<String> strings = Arrays.asList(ids.split(","));
        removeBatchByIds(strings);
        resultService.remove(Wrappers.<TopicResult>lambdaQuery().in(TopicResult::getTopicId, strings));
    }

}
